package org.mili.ticketeer.report;

import org.apache.commons.io.FileUtils;
import org.mili.core.templating.TemplateStore;
import org.mili.ticketeer.dto.Ticket;
import org.mili.application.service.ServiceException;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class BoardTicketGenerator extends AbstractTicketGenerator {

    public BoardTicketGenerator(ReportEngine reportEngine, TemplateStore templateStore) {
        super(reportEngine, templateStore);
    }

    public void generate() throws IOException, ServiceException {
        System.out.println("generate");
        StringBuilder s1 = new StringBuilder();
        String temp1 = templateStore.get("tickets_board_Tickets");
        String temp2 = templateStore.get("tickets_board_Ticket");

        StringBuilder s2 = new StringBuilder();
        List<Ticket> tickets = reportEngine.getBoardTickets();
        for(Ticket ticket : tickets) {
            s2.append(reportEngine.replaceWithTicket(temp2, ticket));
        }

        s1.append(temp1
                .replace("${Ticket}", s2)
        );

        FileUtils.writeStringToFile(new File("./tmp/out/punkte_vorstand.html"),
                templateStore.get("tickets_board_Main")
                        .replace("${Tickets}", s1)
                        .replace("${date}", format.format(new Date()))
                        .replace("${title}", "Punkte f&uuml;r den Vorstand")
        );
    }
}
